<?php
/**
 * @author Demianchuk Dmitro
 * @email d.demianchuk@gmail.com
 * @date 09.01.12
 * @version 1.0
 * @package adminArea
 */
class CheckBoxInput extends EditorComponent
{
	/**
	 * @param CActiveRecord $model
	 * @return CActiveRecord
	 */
	public function save($model){
		$name = $this->name;
		if(isset($_POST['_form'][$this->name]) && $_POST['_form'][$this->name]=='on'){
			$model->$name = 1;
		}else{
			$model->$name = 0;
		}
		return $model;
	}

	/**
	 * @return string
	 */
	public function show(){
		$value = '';
		if($this->itemId!=0){
			$name = $this->name;
			if($this->current->$name>0){
				$value = 'checked="checked"';
			}
		}else{
			if($this->model->getTableSchema()->columns[$this->name]->defaultValue==1){
				$value = 'checked="checked"';
			}
		}
		return '<input type="checkbox" '.$value.' name="_form['.$this->name.']" />';
	}

	/**
	 * @return boolean
	 */
	public function isNull(){
		return false;
	}
}
